package com.liutaw.nanjingair.views.activities.products;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.codetroopers.betterpickers.radialtimepicker.RadialTimePickerDialogFragment;
import com.codetroopers.betterpickers.timepicker.TimePickerBuilder;
import com.codetroopers.betterpickers.timepicker.TimePickerDialogFragment;
import com.liutaw.nanjingair.R;
import com.liutaw.nanjingair.presenter.products.IRadarPresenter;
import com.liutaw.nanjingair.presenter.products.RadarPresenter;
import com.liutaw.nanjingair.views.activities.AndroidApplication;
import com.liutaw.nanjingair.views.activities.base.BaseActivity;
import com.liutaw.nanjingair.views.viewcomponent.banner.NetworkImageHolderView;
import com.liutaw.navigationbarlib.NaviBarBuilder;

import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SatelliteActivity extends BaseActivity  implements IRadarView{

    @Bind(R.id.text_start_time)
    TextView textStartTime;
    @Bind(R.id.view_start_time)
    LinearLayout viewStartTime;
    @Bind(R.id.text_end_time)
    TextView textEndTime;
    @Bind(R.id.view_end_time)
    LinearLayout viewEndTime;
    @Bind(R.id.banner)
    ConvenientBanner banner;
    @Bind(R.id.view_left)
    FrameLayout viewLeft;
    @Bind(R.id.view_play)
    FrameLayout viewPlay;
    @Bind(R.id.view_right)
    FrameLayout viewRight;
    private IRadarPresenter presenter;


    public static Intent getCallingIntent(Context context) {
        return new Intent(context, SatelliteActivity.class);
    }
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.layout_satellite);
//    }

    @Override
    protected int getLayoutResId() {
        return R.layout.layout_satellite;
    }

    @Override
    protected void initNaviBar(NaviBarBuilder naviBarBuilder) {

    }

    @Override
    protected String getCusTitle() {
        return "卫星云图";
    }

    @Override
    protected void initOnCreate() {
        presenter = new RadarPresenter(this);
        presenter.requestPicUrlLists();
    }

    @Override
    protected void initCusView(View view) {
        ButterKnife.bind(this, view);
    }

    @Override
    public void onPicsLoaded(final List<String> picUrls) {
        banner.setPages(new CBViewHolderCreator<NetworkImageHolderView>() {
            @Override
            public NetworkImageHolderView createHolder() {
                return new NetworkImageHolderView(picUrls.size());
            }
        }, picUrls).setPageIndicator(new int[]{R.drawable.ic_page_indicator, R.drawable.ic_page_indicator_focused});
        banner.getViewPager().setCurrentItem(0, false);
        banner.setPointViewVisible(false);
        banner.getViewPager().setCanScroll(false);
//        banner.setPageTransformer(ViewPager.PageTransformer)
    }

    @Override
    public void onStartTimeTextLoaded(String startTime) {
        textStartTime.setText(startTime);
    }

    @Override
    public void onEndTimeTextLoaded(String endTime) {
        textEndTime.setText(endTime);
    }
//
//    @Override
//    public void onTimeDateInit(Date startDate, Date endDate) {
//        this.startDate = startDate;
//        this.endDate = endDate;
//    }


    @OnClick(R.id.view_start_time)
    public void clickStartTime() {
//        RadialTimePickerDialogFragment rtpd = new RadialTimePickerDialogFragment()
//                .setOnTimeSetListener(new RadialTimePickerDialogFragment.OnTimeSetListener() {
//                    @Override
//                    public void onTimeSet(RadialTimePickerDialogFragment dialog, int hourOfDay, int minute) {
//                        AndroidApplication.getInstance().showToastMessage(hourOfDay+"-"+minute);
//                    }
//                })
//                .setStartTime(10, 10).setForced24hFormat()
//                .setThemeDark(true);
//        rtpd.show(getSupportFragmentManager(),"timePickerDialogFragment");

        final TimePickerBuilder tpb = new TimePickerBuilder()
                .setFragmentManager(getSupportFragmentManager())
                .setStyleResId(R.style.BetterPickersDialogFragment).addTimePickerDialogHandler(new TimePickerDialogFragment.TimePickerDialogHandler() {
                    @Override
                    public void onDialogTimeSet(int reference, int hourOfDay, int minute) {
                        if (!presenter.isValide(hourOfDay+"",minute+"",true)) {
                            AndroidApplication.getInstance().showToastMessage(hourOfDay+":"+minute+"必须在范围内");
                        } else {
                            AndroidApplication.getInstance().showToastMessage(hourOfDay+":"+minute+"作为新的开始时间");
                        }
                    }
                });
        tpb.show();
    }

    @OnClick(R.id.view_end_time)
    public void clickEndTime() {
        TimePickerBuilder tpb = new TimePickerBuilder()
                .setFragmentManager(getSupportFragmentManager())
                .setStyleResId(R.style.BetterPickersDialogFragment).addTimePickerDialogHandler(new TimePickerDialogFragment.TimePickerDialogHandler() {
                    @Override
                    public void onDialogTimeSet(int reference, int hourOfDay, int minute) {
                        if (!presenter.isValide(hourOfDay+"",minute+"",false)) {
                            AndroidApplication.getInstance().showToastMessage(hourOfDay+":"+minute+"必须在范围内");
                        } else {
                            AndroidApplication.getInstance().showToastMessage(hourOfDay+":"+minute+"作为新的结束时间");
                        }
                    }
                });
        tpb.show();
    }

    @OnClick(R.id.view_left)
    public void clickLeftView() {
        if (banner.getCurrentItem() != 0) {
            banner.getViewPager().setCurrentItem(banner.getCurrentItem() - 1, false);
        } else {
            banner.getViewPager().setCurrentItem(banner.getViewPager().getLastItem(), false);
        }
    }

    private long current = 0L;
    private boolean isturning = false;
    @OnClick(R.id.view_play)
    public void clickPlayView() {
        current = System.currentTimeMillis();
        if (isturning) {
            isturning =false;
        } else {
            isturning = true;
            startTurn(current);
        }

    }

    public void startTurn(final long temp) {
        banner.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (current == temp) {
                    clickRightTime();
                    startTurn(temp);
                }
            }
        }, 1000);
    }

    @OnClick(R.id.view_right)
    public void clickRightTime() {
        if (banner.getCurrentItem() != banner.getViewPager().getLastItem()) {
            banner.getViewPager().setCurrentItem(banner.getCurrentItem() + 1, false);
        } else {
            banner.getViewPager().setCurrentItem(0, false);
        }
    }
}
